layui.use(['form', 'layedit', 'laydate','jquery'], function() {
    var form = layui.form
        , layer = layui.layer
        , layedit = layui.layedit
        , laydate = layui.laydate
        ,$=layui.$;

    //自定义验证规则
    form.verify({
        title: function (value) {
            if (value.length < 5) {
                return '标题至少得5个字符啊';
            }
        }
        , pass: [
            /^[\S]{6,12}$/
            , '密码必须6到12位，且不能出现空格'
        ]
        , content: function (value) {
            layedit.sync(editIndex);
        }
    });

    //监听指定开关
    form.on('switch(switchTest)', function (data) {
        layer.msg('开关checked：' + (this.checked ? 'true' : 'false'), {
            offset: '6px'
        });
        layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
    });

    //监听提交
    form.on('submit(tijiao)', function(data){
        $.ajax({
            url:'/logisticsPrice/addOrUpdate',
            dataType:'json',
            type:'POST',
            async: false,
            data: $('#priceForm').serialize(),
            success: function(resp){
                if (resp.state == 1){
                    layerClose();
                    layer.msg(resp.msg);
                    parent.table.reload('dataListTable');
                    parent.layer.msg("成功");
                }else {
                    layer.msg("失败");
                }
            },
            error:function(response){
                console.log(response);
            }
        });
        return false;
    });

    let id=getQueryString("id");
    if (id!=null){
        let url="/logisticsPrice/showUpdate";
        let data1={id:id};
        $.post(url,data1,function (resp) {
            if(resp.state==1){
                form.val('priceForm', resp.data);
                form.render();
            }
        },"json");
    }

    $('#close').on("click",function (){
        layerClose();
    })

    $.ajax({
        url:'/logisticsPrice/createOption',
        dataType:'json',
        type:'POST',
        async: false,
        success: function(resp){
            if (resp.state==1){
                let d=resp.data;
                let select=document.getElementById("priceDeparture");
                let select1=document.getElementById("priceBourn");
                for (let i = 0; i < d.length; i++) {
                    console.info("接收到的数据为"+d[i].areaName);
                    let op=new Option(d[i].areaName,d[i].areaId);
                    select.add(op);
                }
                for (let i = 0; i < d.length; i++) {
                    console.info("接收到的数据为"+d[i].areaName);
                    let op=new Option(d[i].areaName,d[i].areaId);
                    select1.add(op);
                }
                form.render('select');
            }
        },
        error:function(response){
            console.log(response);
        }
    });

})

/**
 得到地址栏的参数
 */
function getQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}

